package com.lry.basic.netty.protobuf.proto;


import java.io.*;

/**
 * @author:刘仁有
 * @desc:
 * @email:953506233@qq.com
 * @data:2019/9/23
 */
public class testProto {
    public static void main(String[] args) throws IOException {
        SUser user = new SUser(1,"lry","0530");
        byte[] serByte = ToBytes(user);
        System.out.println(serByte.length);
        SUser us = (SUser) toObject(serByte);
        System.out.println(us.id);
    }

    public static byte[] ToBytes(SUser user) throws IOException {
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        ObjectOutputStream oos = new ObjectOutputStream(bos);
        oos.writeObject(user);
        oos.flush();
        byte[] bytes = bos.toByteArray();
        oos.close();
        bos.close();
        return bytes;
    }

    public static Object toObject (byte[] bytes) {
        Object obj = null;
        try {
            ByteArrayInputStream bis = new ByteArrayInputStream (bytes);
            ObjectInputStream ois = new ObjectInputStream (bis);
            obj = ois.readObject();
            ois.close();
            bis.close();
        } catch (IOException ex) {
            ex.printStackTrace();
        } catch (ClassNotFoundException ex) {
            ex.printStackTrace();
        }
        return obj;
    }


    static class SUser implements Serializable {
        int id;
        String username;
        String password;
        public SUser(){}
        public SUser(int id, String username, String password) {
            this.id = id;
            this.username = username;
            this.password = password;
        }
    }
}


